package wmh.day01092.dao.Impl;


import wmh.day01092.dao.Tour_RouteDao;
import wmh.day01092.po.Tour_Route;
import wmh.day01092.utils.BaseDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Tour_RouteDaoImpl extends BaseDao implements Tour_RouteDao{

    @Override
    public List<Tour_Route> queryTour_Route(Map<String,Object> map){
        //创建List集合对象用来存储出行计划集合数据
        List<Tour_Route> tour_routeList = new ArrayList();
        //创建Schedule对象
        Tour_Route  tour_route = null;
        StringBuffer sql = new StringBuffer();
        //定义查询sql
        sql.append("SELECT TR_ID,TR_NAME,TR_TYPE,TR_PRICE,TR_PHONE,TR_USER,TO_CHAR(CREATE_TIME,'yyyy-MM-dd') CREATE_TIME FROM tour_route");
        sql.append("  WHERE 1 = 1 ");
        if (map != null && map.size() > 0 ) {
            //条件查询 1  景点编号
            Object tour_routeId = map.get("tour_routeId") ;
            //条件查询2   游玩日期
            Object tour_routeTime = map.get("tour_routeTime");
            //条件查询3  线路类型
            Object tour_routeType = map.get("tour_routeType");
            // 用户只查询条件1
            if ( tour_routeId  != null  && tour_routeTime == null  ) {
                sql.append(" AND  TR_ID = ? ");
                //调用公共的查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{ map.get("tour_routeId") });
            }

            // 用户只查询条件2
            if( tour_routeTime != null  && tour_routeTime == null ){
                sql.append(" AND CREATE_TIME = TO_DATE(?,'yyyy-MM-dd')  ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{tour_routeTime});
            }

            //  用户查询条件1 +  查询条件2 (组合查询)
            if ( tour_routeId != null && tour_routeTime != null ){
                sql.append(" AND  TR_ID = ? ");
                sql.append(" AND CREATE_TIME = TO_DATE(?,'yyyy-MM-dd') ");
                //调用公共查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{tour_routeId,tour_routeTime});
            }

            // 条件查询3

            if ( tour_routeType != null  && tour_routeId == null  ) {
                sql.append(" AND TR_TYPE = ? ");
                //调用公共的查询方法
                rs = super.executeQuery(sql.toString(),new Object[]{tour_routeId,tour_routeTime, tour_routeType});
            }
            // TR_Name
            if( map.get("TR_NAME") != null){
                sql.append(" AND TR_NAME = ?  ");
                rs = super.executeQuery(sql.toString(),new Object[]{ map.get("TR_NAME")});
            }
        }else{
            //查询所有
            //调用公共的查询方法
            rs = super.executeQuery(sql.toString());
        }
        try {
            while (rs.next()) {
                //循环遍历rs结果集
                tour_route = new Tour_Route();
                tour_route.setTR_ID(rs.getInt("TR_ID"));
                tour_route.setTR_NAME(rs.getString("TR_NAME"));
                tour_route.setTR_TYPE(rs.getInt("TR_TYPE"));
                tour_route.setTR_PRICE(rs.getInt("TR_PRICE"));
                tour_route.setTR_PHONE(rs.getString("TR_PHONE"));
                tour_route.setTR_USER(rs.getString("TR_USER"));
                tour_route.setCREATE_TIME(rs.getString("CREATE_TIME"));
                //将循环遍历的单个对象存储到集合对象中
                tour_routeList.add(tour_route);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return tour_routeList;
    }

    @Override
    public int updateTour_RouteByType(Tour_Route tour_route) {
        String sql = " UPDATE  Tour_Route  SET TR_TYPE = ? WHERE TR_ID = ? ";
        return super.executeUpdate(sql, new Object[]{tour_route.getTR_TYPE(), tour_route.getTR_ID()});
    }

    @Override
    //添加线路信息
    public int saveTour_Route(Tour_Route tour_route) {

        int addResult = 0;
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO Tour_Route(TR_ID,TR_NAME,TR_TYPE,TR_PRICE,TR_PHONE,TR_USER,CREATE_TIME) VALUES (SEQ_TOUR_ROUTE_TR_ID.NEXTVAL,?,?,?,?,?,TO_DATE(?,'yyyy-MM-dd'))");
        addResult = super.executeUpdate(String.valueOf(sql),new Object[]{tour_route.getTR_NAME(), tour_route.getTR_TYPE(),tour_route.getTR_PRICE(),tour_route.getTR_PHONE(),tour_route.getTR_USER(),tour_route.getCREATE_TIME()});
        return addResult;
    }

    /** @Override
    public int deleteDeptBytesById(int scheduleId) {
    String sql = "DELETE * FROM schedule WHERE id = ?";
    return super.executeUpdate(sql,new Object[]{scheduleId});
    }
     **/
}